<?php

function cpx_vote_coupon($coupon_id, $show_in_popup = false)
{
    $cp_rate = get_post_meta($coupon_id, 'cp_rate_metadata', true);
    if(!$show_in_popup)
        $id_result_vote = 'cp-rate-'.$coupon_id;
    else
        $id_result_vote = 'cp-rate-p-'.$coupon_id;
    ?>
    <div class="span2 voting">
        <div class="vopop">
        <?php if(!cpx_is_voted($coupon_id)): ?>
            <div title="It worked" class="voplefttop" onclick="vote_up(<?php echo $coupon_id;?>,'up')"></div>
            <div title="It didn't work" class="vopleftdown" onclick="vote_up(<?php echo $coupon_id;?>,'down')"></div>
         <?php endif; ?>
            <div class="voright" id="<?php echo $id_result_vote; ?>"><?php if(is_numeric($cp_rate)) echo $cp_rate; ?>% SUCCESS</div>
        </div>        
    </div>
<?php
}

add_action('wp_head', 'ajax_vote_coupon');
function ajax_vote_coupon()
{
?>
    <script type="text/javascript">
    function vote_up(cp_id, action)
    {
        jQuery(document).ready(function($)
        {
            var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
            
            values = new Array();
            values[0] = cp_id;
            values[1] = action;
            
            var data = {
    		action: 'rate_coupon',
    		value_send: values
            };
            
            $.post(ajaxurl, data, function(result) {
                if(result == 'voted')
                    alert('You already voted this coupon');
                else
                {
                    $('#cp-rate-' + cp_id).text(result + '% SUCCESS');
                    $('#cp-rate-p-' + cp_id).text(result + '% SUCCESS');
                }
                    
            })
            
        })
    }
    
    </script>
<?php
    
}
// CALLBACK FUNC
add_action('wp_ajax_rate_coupon', 'rate_cp_func');
function rate_cp_func()
{    
    $arr_received = $_POST['value_send'];
    $coupon_id = $arr_received[0];
    $action = $arr_received[1];
    $cp_like = get_post_meta($coupon_id, 'like_metadata', true);
    $cp_dislike = get_post_meta($coupon_id, 'dislike_metadata', true);;
    
    // If user try again vote this coupon
    if(cpx_is_voted($coupon_id))
    {
        echo 'voted';
        die();
    }
    
    // Add new ip metadata to this coupon
    $old_ips = get_post_meta($coupon_id, 'ip_voted', true);
    $old_ips .= '|' . $_SERVER['REMOTE_ADDR'];
    
    if(!add_post_meta($coupon_id, 'ip_voted', $old_ips, true))
        update_post_meta($coupon_id, 'ip_voted', $old_ips);
    // Process vote coupon    
    if($action == 'up')
    {
        $new_like = $cp_like + 1;
        if(!add_post_meta($coupon_id, 'like_metadata', $new_like, true))
            update_post_meta($coupon_id, 'like_metadata', $new_like);
            
        $new_cp_rate = cpx_calculate_coupon_rate($new_like, $cp_dislike);
        if(!add_post_meta($coupon_id, 'cp_rate_metadata', $new_cp_rate, true))
            update_post_meta($coupon_id, 'cp_rate_metadata', $new_cp_rate);
        echo $new_cp_rate;
    }
    else if($action == 'down')
    {
        $new_dislike = $cp_dislike + 1;
        if(!add_post_meta($coupon_id, 'dislike_metadata', $new_dislike, true))
            update_post_meta($coupon_id, 'dislike_metadata', $new_dislike);
            
        $new_cp_rate = cpx_calculate_coupon_rate($cp_like, $new_dislike);        
        if(!add_post_meta($coupon_id, 'cp_rate_metadata', $new_cp_rate, true))
            update_post_meta($coupon_id, 'cp_rate_metadata', $new_cp_rate);
        echo $new_cp_rate;
    }
    
    die();
}

